<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/5/12
 * Time: 4:32 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Payments_PaymentMethodsStatuses extends Page {
	/**
	 * @var Db_Payments_PaymentMethodsStatuses
	 */
	protected $tableObject;

	function __construct() {
		$this->idTableObject = get('id', 'int');
		$this->classTableObject = 'Db_Payments_PaymentMethodsStatuses';
		$this->actionsTableObject = array(
			'addEditAction',
			'saveAction',
			'ajaxLoadDataAction'
		);
		parent::__construct();
	}

	public function defaultAction() {
		self::$TPL->appendJsFile('admin/payment_methods_statuses');
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');

		self::$TPL->setBodyTemplate('list_payment_methods_statuses');
		return array(
			'type' => 'display'
		);
	}

	public function addEditAction() {
		perm('PAYMENT_METHODS_STATUSES_EDIT', self::$PAGE['p_url']);

		$types = Db_Payments_PaymentMethodsStatuses::getEnumValues('pms_type');
		self::$TPL->assign('types', $types);

		$securityTypes = Db_Payments_PaymentMethodsStatuses::getEnumValues('pms_security_type');
		self::$TPL->assign('securityTypes', $securityTypes);

		$paymentMethods = Db_Payments_PaymentMethods::findAll();
		self::$TPL->assign('paymentMethods', $paymentMethods);

		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' "' . $this->tableObject->objectArray->pms_status_id . '"', '?action=addEdit', true);
			self::$TPL->assign('paymentMethodsStatuses', $this->tableObject->objectArray);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
		}

		self::$TPL->setBodyTemplate('add_edit_payment_methods_statuses');
		return array(
			'type' => 'display'
		);
	}

	public function ajaxLoadDataAction() {

		$allColumns = array('nr', 'pms_status_id', 'pms_status_description', 'pms_security_type', 'pms_type', 'pm_name');
		$searchColumns = array('pms_status_id', 'pms_status_description', 'pms_security_type', 'pms_type', 'pm_name');

		list($searchQuery,$orderBy,$limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, "Db_Payments_PaymentMethodsStatuses", "getTotalPaymentMethodsStatuses");

		$allPaymentMethodsStatuses = $this->tableObject->getAllPaymentMethodsStatuses($limit, $searchQuery, $orderBy);
		self::$TPL->assign('allPaymentMethodsStatuses', $allPaymentMethodsStatuses);

		return array(
			'type' => 'encoded_json',
			'content' => self::$TPL->display('json_list_payment_methods_statuses', true)
		);
	}

	public function saveAction() {
		perm('PAYMENT_METHODS_STATUSES_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		$this->tableObject->pms_payment_method_id = post('pms_payment_method_id', 'int');
		$this->tableObject->pms_security_type = post('pms_security_type', 'str');
		$this->tableObject->pms_status_description = post('pms_status_description', 'str');
		$this->tableObject->pms_status_id = post('pms_status_id', 'str');
		$this->tableObject->pms_type = post('pms_type', 'str');

		if(!empty($this->tableObject->pms_payment_method_id) && !empty($this->tableObject->pms_security_type) && !empty($this->tableObject->pms_status_id) && !empty($this->tableObject->pms_type)) {
			if(!$this->tableObject->save()) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function deleteAction() {
		perm('PAYMENT_METHODS_STATUSES_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');

		if(!Db_Payments_PaymentMethodsStatuses::abstractDelete($this->idTableObject)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

}
